package com.liu.usky.rlzp.uskyrlzp.controller;


import javax.servlet.http.HttpServletRequest;

import com.liu.usky.rlzp.uskyrlzp.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.liu.usky.rlzp.uskyrlzp.entity.User;
import com.liu.usky.rlzp.uskyrlzp.service.IEquityService;
import com.liu.usky.rlzp.uskyrlzp.service.IUserService;
import com.liu.usky.rlzp.uskyrlzp.util.R;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author lcjzl
 * @since 2020-01-10
 */
@RestController
@RequestMapping("/uskyrlzp/user")
public class UserController extends BaseController {
	@Autowired
	private IUserService iUserService;
	@Autowired
	private IEquityService iEquityService;
	
	@PostMapping(value="login",produces="application/json")
	public String login(@RequestParam(required=true) String userName, 
			@RequestParam(required=true) String password ,HttpServletRequest request) throws JsonProcessingException {
		
		User user = iUserService.getByAccount(userName);
		if(user == null) {
			return R.FAILURE("用户名或密码错误", null);
		}
		String pwd = user.getPassword();
		if(!pwd.equals(password)) {
			return R.FAILURE("用户名或密码错误", null);
		}
		
		user.setEquitys(iEquityService.getByUserId(user.getId()));
		System.out.println(request.getSession().getId());
		request.getSession().setAttribute("user", user);
		return R.OK("登录成功");
	}
	
	@PostMapping(value="loginOut",produces="application/json")
	public String loginOut(HttpServletRequest request) throws JsonProcessingException {
		request.getSession().setAttribute("user", null);
		return R.OK("登出成功");
	}
}
